package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.SetMeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.Setmeal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        List<Setmeal> resultlist = setmealMapper.query(dto);
        Page<Setmeal> pageResult = (Page<Setmeal>) resultlist;
        return new PageResult(pageResult.getTotal(),pageResult.getResult());
    }

    @Transactional
    @Override
    public void addSetmeal(SetmealDTO dto) {
        SetMeal setMeal = new SetMeal();
        BeanUtils.copyProperties(dto,setMeal);
        setmealMapper.addSetmeal(setMeal);

        //检查套餐内是否有菜品，若无则不进行添加操作
        if(Objects.nonNull(dto.getSetmealDishes()) && !dto.getSetmealDishes().isEmpty()){
            Long setmealId = setMeal.getId();
            for (SetmealDish setmealDish : dto.getSetmealDishes()) {
                setmealDish.setSetmealId(setmealId);
            }
            setmealDishMapper.addSetmealDishes(dto.getSetmealDishes());
        }
    }

    @Override
    @Transactional
    public void deleteAll(List<Long> ids) {
        Integer cnt;
        cnt = setmealMapper.checkStatus(ids);
        if(Objects.nonNull(cnt) && cnt > 0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }
        setmealMapper.deleteAll(ids);
        setmealDishMapper.deleteByMealIds(ids);
    }

    @Override
    public Setmeal querySetMealById(Long id) {
        //获取SetMealVO
        Setmeal setmeal = setmealMapper.queryById(id);
        //获取SetMealDishes
        List<SetmealDish> dishList = setmealDishMapper.queryById(id);
        //封装为VO对象返回
        setmeal.setSetmealDishes(dishList);
        return setmeal;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //更新套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //删除套餐中的菜品列表
        List<Long> temp = new ArrayList<>();
        temp.add(setmealDTO.getId());
        setmealDishMapper.deleteByMealIds(temp);
        //重新加入菜品列表
        if(Objects.nonNull(setmealDTO.getSetmealDishes()) && !setmealDTO.getSetmealDishes().isEmpty()){
            Long id = setmealDTO.getId();
            for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.addSetmealDishes(setmealDTO.getSetmealDishes());
        }

    }

    @Override
    public void updateSetmealStatus(Integer status, Long id) {
        setmealMapper.updateSetmealStauts(status,id);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
